package com.resource.unit
{
	import com.resource.animation.Animation;
	import com.resource.animation.AnimationCacher;
	import com.resource.animation.AnimationError;
	import com.resource.animation.AnimationEvent;
	import com.resource.animation.IAnimation;
	
	import flash.display.Sprite;
	
	import spark.effects.Animate;

	/**
	 * 一个部件的一套装备的整套动作
	 * 不考虑加载资源时间导致播放同步问题
	 */
	public class ActionUnit extends Sprite implements IAnimation
	{
		public static const STAND:String = "stand";
		public static const RUN:String = "run";
		public static const ATTACK:String = "attack";
		public static const HURT:String = "hurt";
		public static const SKILL:String = "skill";
		public static const DEFENCE:String = "defence";
		public static const JUMP:String = "jump";
		public static const DIE:String = "die";
		
		public static const ACT_ARR:Array = ["stand", "run", "attack", "hurt", "skill", "defence", "jump", "die"];
		
		private var _actionDic:Object = {};
		private var _resUrl:String; // 资源路径
		private var _act:String; // 当前动作
		private var _dir:int = 1; // 当前方向
		private var _endCallback:Function; // 动画结束回调
		private var _loadComplete:Function; // 预加载加载完成回调
		private var _preLoadNum:int = -1;
		
		public function ActionUnit()
		{
			super();
		}
		
		/**
		 * 获得当前动作的动画对象
		 */
		public function getAnimation():Animation{
			return _actionDic[_act];
		}

		public function enterFrame():void{
			if(_actionDic[_act]){
				(_actionDic[_act] as Animation).enterFrame();
			}
		}
		
		public function set act(value:String):void{
			if(value != _act){
				if(_actionDic[_act]){
					var oldAnima:Animation = _actionDic[_act] as Animation;
					removeChild(oldAnima);
				}
			}
			_act = value;
			if(_actionDic[_act]){
				var anima:Animation = _actionDic[_act] as Animation;
				anima.scaleX = _dir;
				addChild(anima);
				anima.gotoAndPlay(0);
			}else{
				var anima:Animation = new Animation;
				anima.scaleX = _dir;
				addChild(anima);
				anima.gotoAndPlay(0);
				_actionDic[_act] = anima;
				
				var animaCacher:AnimationCacher = AnimationCacher.getInstance();
				var data:Object = animaCacher.getAnimation(_resUrl + _act + ".dat");
				if(data){
					anima.data = data;
				}else{
					animaCacher.addEventListener(_resUrl + _act + ".dat", onLoaded);
					animaCacher.addEventListener(_resUrl + _act + ".daterror", onError);
					animaCacher.load(_resUrl + _act + ".dat");	
				}
			}
			endCallback = _endCallback;
		}
		
		/**
		 * 预加载，加载全套资源
		 */
		public function preLoad(loadComplete:Function=null):void{
			_loadComplete = loadComplete;
			_preLoadNum = ACT_ARR.length;
			for(var i:int=0; i<ACT_ARR.length; i++){
				var animaCacher:AnimationCacher = AnimationCacher.getInstance();
				animaCacher.addEventListener(_resUrl + ACT_ARR[i] + ".dat", onPreLoaded);
				animaCacher.addEventListener(_resUrl + ACT_ARR[i] + ".daterror", onPreError);
				animaCacher.load(_resUrl + ACT_ARR[i] + ".dat");	
			}
		}
		
		private function onPreLoaded(event:AnimationEvent):void{
			var animaCacher:AnimationCacher = AnimationCacher.getInstance();
			animaCacher.removeEventListener(event.type, onLoaded);
			animaCacher.removeEventListener(event.type, onError);
			_preLoadNum--;
			if(_preLoadNum == 0 && _loadComplete){
				_loadComplete.apply();
				_preLoadNum = -1;
			}
		}
		
		private function onPreError(event:AnimationError):void{
			var animaCacher:AnimationCacher = AnimationCacher.getInstance();
			animaCacher.removeEventListener(event.type, onLoaded);
			animaCacher.removeEventListener(event.type, onError);
			_preLoadNum--;
			if(_preLoadNum == 0 && _loadComplete){
				_loadComplete.apply();
			}		
		}
		
		private function onLoaded(event:AnimationEvent):void{
			var data:Object = event.data;
			for(var k:* in _actionDic){
				if(event.type == _resUrl + k + ".dat"){
					var animaCacher:AnimationCacher = AnimationCacher.getInstance();
					animaCacher.removeEventListener(_resUrl + k + ".dat", onLoaded);
					animaCacher.removeEventListener(_resUrl + k + ".daterror", onError);
					var anima:Animation = _actionDic[k] as Animation;
					anima.data = data;
					return;
				}
			}
		}
		
		private function onError(event:AnimationError):void{
			for(var k:* in _actionDic){
				if(event.type == _resUrl + k + ".daterror"){
					var animaCacher:AnimationCacher = AnimationCacher.getInstance();
					animaCacher.removeEventListener(_resUrl + k + ".dat", onLoaded);
					animaCacher.removeEventListener(_resUrl + k + ".daterror", onError);
					return;
				}
			}
		}
		
		public function set endCallback(value:Function):void{
			_endCallback = value;
			for(var k:* in _actionDic){
				var anima:Animation = _actionDic[k] as Animation;
				anima.endCallback = _endCallback;
			}
		}
		
		public function get endCallback():Function{
			return _endCallback;
		}
		
		public function get act():String{
			return _act;
		}
		
		public function getCurrentAct():Animation{
			return _actionDic[_act];
		}
		
		public function getAct(actName:String):Animation{
			return _actionDic[actName];
		}
		/**
		 * 设置方向
		 */
		public function set dir(value:int):void{
			if(value == -1){
				_dir = -1;
			}else{
				_dir = 1;
			}
			if(_actionDic[_act]){
				var anima:Animation = _actionDic[_act] as Animation;
				anima.scaleX = _dir;
			}
		}
		
		public function get dir():int{
			return _dir;
		}
		
		public function get resUrl():String
		{
			return _resUrl;
		}
		
		public function set resUrl(value:String):void
		{
			_resUrl = value;
		}
	}
}